package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.ViewPagerAdapter;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.shouye.EnterpriseViewReturn;
import com.tangkun.environment.fragment.home.JiChuShuJuFragment;
import com.tangkun.environment.fragment.home.XunChaShuJuFragment;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业中心
 */

public class QiYeZhongXinActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.rb_qiyezhongxinr_jichushuju)
    RadioButton rbQiyezhongxinrJichushuju;
    @BindView(R.id.rb_qiyezhongxinr_xunchashuju)
    RadioButton rbQiyezhongxinrXunchashuju;
    @BindView(R.id.rg_qiyezhongxin)
    RadioGroup rgQiyezhongxin;
    @BindView(R.id.title)
    TextView title;

    private Context mContext;
    /**
     * 企业ID
     */
    private String id = "";
    /**
     * 企业名称
     */
    private String name = "";
    /**
     * 当前选中fragment索引
     */
    private int currentPosition = 0;
    /**
     * Fragment集合
     */
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_qiyezhongxin);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        id = getIntent().getStringExtra("id");//传递过来的ID
        name = getIntent().getStringExtra("name");//传递过来的企业名称
        initListener();
        enterpriseView();
    }

    private void initListener() {
        //切换
        rgQiyezhongxin.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb_qiyezhongxinr_jichushuju:
                        viewPager.setCurrentItem(0, true);
                        break;
                    case R.id.rb_qiyezhongxinr_xunchashuju:
                        viewPager.setCurrentItem(1, true);
                        break;

                }
            }
        });
    }

    /**
     * 企业中心
     */
    private void enterpriseView() {
        fromNetwork.enterpriseView(id).compose(new RxActivityHelper<EnterpriseViewReturn>().ioMain(QiYeZhongXinActivity.this, false))
                .subscribe(new MySubscribe<EnterpriseViewReturn>(mContext, false) {
                    @Override
                    protected void success(EnterpriseViewReturn resultBean) {
                        if (resultBean.data != null) {
                            //街道巡查可见举报按钮
                            if (Session.getInstance().getUserRole().equals("街道巡查员")) {
                                next.setText("举报");
                                next.setVisibility(View.VISIBLE);
                            } else {
                                next.setText("举报");
                                next.setVisibility(View.GONE);
                            }
                            if (resultBean.data.owner == 1) {
                                //是我自己的企业
                                rgQiyezhongxin.setVisibility(View.VISIBLE);
                                title.setText("企业中心");
                                title.setVisibility(View.GONE);
                                initFragments(resultBean.data.owner);
                            } else {
                                //不是我自己的企业
                                rgQiyezhongxin.setVisibility(View.GONE);
                                title.setText("企业中心");
                                title.setVisibility(View.VISIBLE);
                                initFragments(resultBean.data.owner);
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }

    /**
     * 初始化Fragments
     *
     * @param owner 1:我的企业 0：不是我的企业
     */
    private void initFragments(int owner) {
        Bundle data1 = new Bundle();
        data1.putString("id", id);
        Bundle data2 = new Bundle();
        data2.putString("id", id);
        //基础数据
        Fragment fragment1 = new JiChuShuJuFragment();
        fragment1.setArguments(data1);
        //巡查数据
        Fragment fragment2 = new XunChaShuJuFragment();
        fragment2.setArguments(data2);
        fragmentList.clear();
        fragmentList.add(fragment1);
        //我的企业才显示巡查数据
        if (owner == 1) {
            fragmentList.add(fragment2);
        } else {
            //ignore
        }
        viewPager.setOffscreenPageLimit(fragmentList.size());//设置缓存数量
        ViewPagerAdapter fragmentPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentPagerAdapter);

        viewPager.setCurrentItem(currentPosition);//设置当前选中项
        //viewpager监听事件
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                if (position == 0) {
                    rgQiyezhongxin.check(R.id.rb_qiyezhongxinr_jichushuju);
                } else {
                    rgQiyezhongxin.check(R.id.rb_qiyezhongxinr_xunchashuju);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.back, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                JumpHelper.finish(mContext);
                break;
            case R.id.next://举报
                Intent intent = new Intent(QiYeZhongXinActivity.this, JuBaoYiChangActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name", name);
                JumpHelper.jump(QiYeZhongXinActivity.this, intent, false);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
